package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    //1、课程列表查询
    @Override
    public PageResult<Course> selectlist(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        //1、设置PageHelper参数
        PageHelper.startPage(page, pageSize);
        //2、调用mapper接口查询
        List<Course> courses = courseMapper.selectlist(name, subject, target);
        //3、解析并封装分页结果
        Page<Course> p = (Page<Course>) courses;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    //2. 添加课程
    @Override
    public void addCourse(Course course) {
        //补全字段
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
        System.out.println("添加课程：" + course);
        log.info("添加课程：{}", course);
    }

    //3、根据ID查询课程数据
    @Override
    public Course getById(Integer id) {
        log.info("查询课程ID为{}的数据", id);
        return courseMapper.selectById(id);
    }

    //4、修改课程数据
    @Override
    public void update(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
        System.out.println("修改课程：" + course);
        log.info("修改课程：{}", course);
    }

    @Override
    public void delete(Integer id) {
        courseMapper.deleteById(id);

        log.info("删除课程：{}", id);
    }

    //6、查询所有课程
    @Override
    public List<Course> listCourses() {
        return courseMapper.listCourses();


    }

    //7、根据学科查询课程列表
    @Override
    public List<Course> getCourseBySubject(Integer subject) {
        return courseMapper.getCourseBySubject(subject);
    }
}
